//
//  GHVisitorView.swift
//  GHWeibo
//
//  Created by LGH on 2017/8/6.
//  Copyright © 2017年 LGH. All rights reserved.
//

import UIKit
import HexColors

class GHVisitorView: UIView {
    
    lazy var maskIconView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var tipLabel = UILabel.labelWith(
        text: "关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray)
    
    lazy var registerBtn = UIButton.buttonWith(
        title: "登录",
        fontSize: 16,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        imageName: "common_button_white_disable",
        target: self,
        action: #selector(login))
    
    lazy var loginBtn = UIButton.buttonWith(
        title: "注册",
        fontSize: 16,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        imageName: "common_button_white_disable",
        target: self,
        action: #selector(register))
    
    var visitorViewInfo: [String: String]?
    {
        
        didSet{
            
            guard let imageName = visitorViewInfo?["imageName"],
                let tipText = visitorViewInfo?["tipText"] else {
                
                    return
            }
            
            if imageName == "" {
                
                startAnimation()
               return
                
            }else{
                
                tipLabel.text = tipText
                iconView.image = UIImage(named: imageName)
                maskIconView.isHidden = true
                houseView.isHidden = true
            }
        }
 
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        animation.isRemovedOnCompletion = false
        iconView.layer.add(animation, forKey: nil)
    }
    
    func login() {
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: GHUserShouldLoginNotification), object: nil)
    }
    
    func register() {
        
        
    }
}



// MARK: - 设置UI
extension GHVisitorView {
    
    func setupUI(){

        backgroundColor = UIColor("ededed")
        self.addSubview(iconView)
        self.addSubview(maskIconView)
        self.addSubview(houseView)
        self.addSubview(tipLabel)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        layout()
    }
    
    func layout() {
        for v in subviews {
            
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        let margin: CGFloat = 20.0
        
        //转轮图像
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: -60))

        //小房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1, constant: 0))
        
        //标签
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: margin))
        
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        
        //遮罩视图
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .bottom, relatedBy: .equal, toItem: registerBtn, attribute: .top, multiplier: 1.0, constant: 0))
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
